﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bilylib;
using System.Data;
using System.IO;
using System.Drawing;

public partial class sitemanagement_AddServices : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["Username"] == null)
            {
                Response.Redirect("HTLogin.aspx");
            }

            BindCate();
        }
    }

    private void BindCate()
    {
        DataTable tbCate = new DataTable();
        string sql = "SELECT CateID, CategoryName FROM Categories WHERE Status =  @Status";
        Select select = Select.newInstance();
        select.SetConnectionName("HoangThinhDataConnectionString");
        select.SetValues(1);
        tbCate = select.SelectCustomParam(sql);

        ddlCate.DataSource = tbCate;
        ddlCate.DataTextField = "CategoryName";
        ddlCate.DataValueField = "CateID";
        ddlCate.DataBind();
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtTitle.Text = "";
        txtContent.Text = "";
    }

    protected void btnPost_Click(object sender, EventArgs e)
    {
        try
        {
            string strGuid = createString(10);
            string location = "images/noimage.gif";
            string date = DateTime.Now.ToString("MM/dd/yyyy");
            String path = Server.MapPath("../images/service");
            string fileSave = path + "/" + strGuid + fileImage.FileName.Replace("'", "");
            int cateID = Convert.ToInt32(ddlCate.SelectedValue);

            if (fileImage.HasFile)
            {
                location = "images/service/" + strGuid + fileImage.FileName.Replace("'", "");
            }

            string sql = "Insert Into Service Values(@CateID, @Title, @ImageUrl, @Content);";
            Insert insert = Insert.newInstance();
            insert.SetConnectionName("HoangThinhDataConnectionString");
            insert.SetParamValues(cateID);
            insert.SetParamValues(txtTitle.Text.Replace("'", ""));
            insert.SetParamValues(location);
            insert.SetParamValues(txtContent.Text);

            if (insert.InsertCustomParam(sql))
            {
                try
                {
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    if (fileImage.HasFile)
                    {
                        fileImage.SaveAs(fileSave);
                    }
                }
                catch
                {
                }

                lblMsg.ForeColor = ColorTranslator.FromHtml("#009900");
                lblMsg.Text = "Thêm nội dung dịch vụ thành công !!";
                txtTitle.Text = "";
                txtContent.Text = "";
                txtTitle.Focus();
            }
            else
            {
                lblMsg.ForeColor = ColorTranslator.FromHtml("Red");
                lblMsg.Text = "Thêm nội dung dịch vụ thất bại !!";
            }
        }
        catch
        {
            lblMsg.ForeColor = ColorTranslator.FromHtml("Red");
            lblMsg.Text = "Lỗi hệ thống !!";
        }
    }

    private string createString(int length)
    {
        string _allowedChars = "abcdefghijkmnopqrstuvwxyz0123456789";
        Random randNum = new Random();
        char[] chars = new char[length];
        int allowedCharCount = _allowedChars.Length;

        for (int i = 0; i < length; i++)
        {
            chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
        }
        return new string(chars);
    }
}